//
//  TSCardListViewController.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/5.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit
import DZNEmptyDataSet
import MJRefresh

class TSCardListViewController: TSBaseViewController,TSCardEmptyViewDelegate   {

    var tableController : TSBaseTableViewController!
    lazy var dataSource = Array<TSCardListModel>()
    lazy var customEmptyView : TSCardEmptyView = {
        var emptyView = TSCardEmptyView.init(frame: CGRect(x: 0, y: 30, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
        emptyView.delegate = self
        return emptyView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configViews()
        setupNavigationTitle(title: "银行卡")
        requestDatas(true)
    }
    
    func didClickBind() {
        bindAction()
    }
    
    @objc func refreshDatas() {
        requestDatas(true)
    }

    @objc func bindAction() {
        
        let vc = TSCardBindViewController()
        vc.delegate = self
        navigationController?.pushViewController(vc, animated: true)
        print("绑定新卡")
    }
}

extension TSCardListViewController : TSCardBindViewControllerDelegate {
    
    func completeBindCard() {
        requestDatas(false)
    }
}

extension TSCardListViewController : UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        133
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        15
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        0.001
    }
}

extension TSCardListViewController : UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(TSCardListTableViewCell.self)) as? TSCardListTableViewCell
        
        if cell == nil {
            cell = TSCardListTableViewCell.init(style: .default, reuseIdentifier: NSStringFromClass(TSCardListTableViewCell.self))
        }
        
        cell?.setupCell(model: dataSource[indexPath.section])
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = UIView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 15))
        header.backgroundColor = WHITE_COLOR
        return header
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        let footer = UIView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 0.001))
        footer.backgroundColor = WHITE_COLOR
        return footer
    }
}

// MARK: - Network Request
extension TSCardListViewController {
    
    func requestDatas(_ showHud : Bool) {
        
        CBToast.showToastAction()
        TSNetworkManager.request(TMSNetworkTools.getCardList, showHUD: showHud, success: { (data) in
            
            CBToast.hiddenToastAction()
            self.dataSource.removeAll()
            let array = data as? Array<Dictionary<String, Any>>
            if array?.count ?? 0 > 0 {
                for item in array! {
                    let tempModel = TSCardListModel.deserialize(from: item)
                    if let model = tempModel {
                        self.dataSource.append(model)
                    }
                }
            }
            self.tableController.tableView.reloadData()
            
        }) { (error) in
            
            CBToast.hiddenToastAction()
        }
    }
}

extension TSCardListViewController {
    
    func configViews() {
        
        tableController = TSBaseTableViewController(style: .grouped)
        tableController.customEmptyView = customEmptyView
        tableController.tableView.backgroundColor = WHITE_COLOR
        tableController.tableView.delegate = self;
        tableController.tableView.dataSource = self
        tableController.tableView.separatorStyle = .none
        self.view.addSubview(tableController.tableView)
        tableController.tableView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(T_NAVBARHEIGHT)
        }
        
        tableController.tableView.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(refreshDatas))
    }
}
